#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: dag.py 
@version:
@time: 2022/05/31 
"""


class DAGNEW:
    from TapClientCore.T04_logic.DagNew.node.clone_node.source import CloneSource
    from TapClientCore.T04_logic.DagNew.node.clone_node.target import CloneTarget
    from TapClientCore.T04_logic.DagNew.relations.relations_clone import RelationsClone
    from TapClientCore.T04_logic.DagNew.node.sync_node.source import SyncSource
    from TapClientCore.T04_logic.DagNew.node.sync_node.target import SyncTarget
    from TapClientCore.T04_logic.DagNew.relations.relations_sync import RelationSync

    def __init__(self):
        self.stages = []

    def clone_edge(
            self,
            source: CloneSource,
            target: CloneTarget,
            relations: RelationsClone
    ):
        # 输入输出id
        source.node['outputLanes'].append(target.node.get('id'))
        target.node['inputLanes'].append(source.node.get('id'))
        # 关系
        target.node['syncObjects'] = [
            relations.syncObjects
        ]
        target.node["table_prefix"] = relations.prefix
        target.node["table_suffix"] = relations.suffix
        target.node["dropType"] = relations.dropType
        target.node['tableNameTransform'] = relations.tableNameTransform
        target.node['fieldsNameTransform'] = relations.fieldsNameTransform

        self.stages.append(source.node)
        self.stages.append(target.node)

    def sync_edge(
            self,
            source: SyncSource,
            target: SyncTarget,
            relations: RelationSync
    ):
        # 输入输出id
        source.node['outputLanes'].append(target.node.get('id'))
        target.node['inputLanes'].append(source.node.get('id'))

        joinTables = {
            'joinType': relations.joinType,
            'joinPath': relations.joinPath,
            'manyOneUpsert': relations.manyOneUpsert,
            'joinKeys': [{"source": m[0], "target": m[1]} for m in relations.joinKeys],
            'arrayUniqueKey': relations.arrayUniqueKey,
            "isArray": relations.isArray,
            'connectionId': '',
            'stageId': '',
            'tableName': '',
        }
        target.node['joinTables'].append(joinTables)

        self.stages.append(source.node)
        self.stages.append(target.node)
